package com.mes_for_xxxxxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mes_for_xxxxxx.entity.Process;
import com.mes_for_xxxxxx.entity.Processflowrouter;
import com.mes_for_xxxxxx.mapper.ProcessflowrouterMapper;
import com.mes_for_xxxxxx.service.IProcessService;
import com.mes_for_xxxxxx.service.IProcessflowrouterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 工艺流程路由表 服务实现类
 * </p>
 *
 * @author 179005
 * @since 2024-09-10
 */
@Service
public class ProcessflowrouterServiceImpl extends ServiceImpl<ProcessflowrouterMapper, Processflowrouter> implements IProcessflowrouterService {

    @Autowired
    IProcessService processService;
    @Override
    public String updStatus(Long id) {
        Processflowrouter processflowrouter = baseMapper.selectById(id);
        if(Objects.isNull(processflowrouter)){
            throw new RuntimeException("id不存在");
        }
        if("启用中".equals(processflowrouter.getStatus())){
            processflowrouter.setStatus("禁用");
        }else if("禁用".equals(processflowrouter.getStatus())){
            processflowrouter.setStatus("启用中");
        }
        baseMapper.updateById(processflowrouter);
        return "更新成功";
    }

    @Override
    public List<Process> listProcessByRouteName(String routerName) {
        QueryWrapper<Processflowrouter>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("router_name",routerName);
        Processflowrouter processflowrouter = baseMapper.selectOne(queryWrapper);
        String routerNumber = processflowrouter.getRouterNumber();

        return listProcess(routerNumber);
    }

    public List<Process> listProcess(String routerNumber) {
        QueryWrapper<Process> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("route_id", routerNumber);
        return processService.list(queryWrapper1);
    }
}
